﻿namespace wToggl
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    using Windows.UI.Core;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Media;

    public static class PhoneExtensions
    {
        public static async Task ShowMessage(this CoreDispatcher dispatcher, string message)
        {
            await dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                async () =>
                    {
                        var md = new MessageDialog(message);
                        await md.ShowAsync();
                    });
        }

        public static async Task Ask(this CoreDispatcher dispatcher, string message, string noButton, IUICommand yesCommand)
        {
            await dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                async () =>
                    {
                        var md = new MessageDialog(message);
                        md.Commands.Add(yesCommand);
                        md.Commands.Add(new UICommand(noButton));
                        md.CancelCommandIndex = 1;
                        await md.ShowAsync();
                    });
        }

        public static async Task Ask(this CoreDispatcher dispatcher, string message, string title, string noButton, IUICommand yesCommand)
        {
            await dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                async () =>
                    {
                        var md = new MessageDialog(message, title);
                        md.Commands.Add(yesCommand);
                        md.Commands.Add(new UICommand(noButton));
                        md.CancelCommandIndex = 1;
                        await md.ShowAsync();
                    });
        }

        public static async Task ShowMessage(this CoreDispatcher dispatcher, string message, string title)
        {
            await dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                async () =>
                    {
                        var md = new MessageDialog(message, title);
                        await md.ShowAsync();
                    });
        }

        public static List<T> FindChildren<T>(this DependencyObject element) where T : DependencyObject
        {
            var list = new List<T>();

            var queue = new Queue<DependencyObject>();
            queue.Enqueue(element);

            while (queue.Count > 0)
            {
                var item = queue.Dequeue();

                var childrenCount = VisualTreeHelper.GetChildrenCount(item);

                for (var i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(item, i);

                    var cast = child as T;
                    if (cast != null)
                    {
                        list.Add(cast);
                    }

                    queue.Enqueue(child);
                }
            }

            return list;
        }

        public static T FindParent<T>(this DependencyObject element) where T : DependencyObject
        {
            var item = element;

            while (item != null)
            {
                item = VisualTreeHelper.GetParent(item);

                var cast = item as T;
                if (cast != null)
                {
                    return cast;
                }
            }

            return null;
        }

        public static T GetDataContext<T>(this object o) where T : class
        {
            var obj = o as FrameworkElement;

            if (obj == null)
            {
                return null;
            }

            var context = obj.DataContext as T;

            if (context == null)
            {
                return null;
            }

            return context;
        }
    }
}
